/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Furgones.java
 *
 * Created on 3/06/2011, 11:51:48 PM
 */

package practicabeta;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Furgones extends javax.swing.JFrame {

    ResultSetTableModel tableModel;
    /** Creates new form Furgones */
    public Furgones() throws IOException {
        //initComponents();
        super("F u r g o n e s  ");
        Inicio();
    }
String consulta = "SELECT idfurgon,kilometraje,placa  FROM furgon";
     public void Inicio() throws IOException
 {


 // consulta para mostrar todo


 try {

// crear el modedlo de la tabla segun la consulta
 // SELECT * FROM authors
 tableModel = new ResultSetTableModel( consulta );
/* final JTextArea consultaArea = new JTextArea( consulta, 3, 100 );
  consultaArea.setWrapStyleWord( true );
 consultaArea.setLineWrap( true );*/

 // agregamos el scroll al area de texto si es necesaria
 /*JScrollPane scrollPane = new JScrollPane( consultaArea,
 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
 scrollPane.setVisible(true);*/


 JButton nuevo = new JButton( "Nuevo" );
 JButton back = new JButton ("Regresar");



 Box box = Box.createHorizontalBox();
 /*box.add( scrollPane );
 box.add(consultaArea);*/

 box.add(back);
 box.add(nuevo);


//creo una tabla con el estilo de tablemodel
 final JTable resultTable = new JTable( tableModel );

 // place GUI components on content pane
 Container c = getContentPane();
 c.add( box, BorderLayout.SOUTH );
 c.add( new JScrollPane( resultTable ),
 BorderLayout.CENTER );

//crea accionlistener

nuevo.addActionListener( new ActionListener() {

 public void actionPerformed( ActionEvent e )
 {

dispose();
NuevoFurgon va = new NuevoFurgon();
va.setVisible(true);
// centra la pantalla
 Dimension pantalla, cuadro;
            pantalla = Toolkit.getDefaultToolkit().getScreenSize();
            cuadro = va.getSize();
            va.setLocation(((pantalla.width - cuadro.width)/2),(pantalla.height - cuadro.height)/2);
va.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


 } // fin del accionperformed del boton

 } // end ActionListener inner class

 );
//accion del boton regresar
back.addActionListener( new ActionListener() {

 public void actionPerformed( ActionEvent e )
 {

dispose();
load va = new load();
va.setVisible(true);
// centramos la pantalla
            Dimension pantalla, cuadro;
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		cuadro = va.getSize();
va.setLocation(((pantalla.width - cuadro.width)/2),(pantalla.height - cuadro.height)/2);
va.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 } // fin del accionperformed del boton

 } // end ActionListener inner class

 ); // end call to addActionListener
resultTable.addMouseListener(new MouseAdapter()
        {

                @Override
            public void mouseClicked(MouseEvent evt) {

                    try {
                        try {
                            Mostrar(evt);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(Furgones.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(Furgones.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(Furgones.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
            public void Mostrar(MouseEvent evt) throws SQLException, FileNotFoundException, IOException
            {
                    int fila = resultTable.rowAtPoint(evt.getPoint());
                if (fila > -1)
                {
                      dispose();
                      Object objeto = tableModel.getValueAt(fila, 0);

                     String ja = objeto.toString();
                     int num=Integer.parseInt(ja);


                      ClienteSelect va = new ClienteSelect (num);
                      va.setVisible(true);
                      va.setResizable(false);

                       Dimension pantalla, cuadro;
                        pantalla = Toolkit.getDefaultToolkit().getScreenSize();
                        cuadro = va.getSize();
                         va.setLocation(((pantalla.width - cuadro.width)/2),(pantalla.height - cuadro.height)/2);
                         va.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            }
        });
 // set window size and display window
 setSize( 500, 250 );
 setVisible( true );
 } // end try

 // catch ClassNotFoundException thrown by
 // ResultSetTableModel if database driver not found
 catch ( ClassNotFoundException classNotFound ) {
 JOptionPane.showMessageDialog( null,
 "Cloudscape driver not found", "Driver not found",
 JOptionPane.ERROR_MESSAGE );

 System.exit( 1 ); // terminate application
 }

 // catch SQLException thrown by ResultSetTableModel
 // if problems occur while setting up database
 // connection and querying database
 catch ( SQLException sqlException ) {
 JOptionPane.showMessageDialog( null,
 sqlException.toString(),
 "Database error", JOptionPane.ERROR_MESSAGE );

 System.exit( 1 ); // terminate application
 }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Furgones().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Furgones.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
